package com.century.sjt.activity.login;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.century.sjt.R;
import com.century.sjt.SjtHelper;
import com.century.sjt.activity.BaseActivity;
import com.century.sjt.activity.MainActivity;
import com.century.sjt.util.Constant;
import com.century.sjt.utils.PreferenceManager;
import com.century.sjt.widget.RippleBackground;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMGroupManager;

/**
 * Created by 炳基 on 2016/8/22.
 */
public class ChooseCountryButtonActivity extends BaseActivity {
    private RippleBackground choose_country;
    private TextView icon_position;
    private AnimationSet mAnimationSet;
    private RelativeLayout rootLayout;
    private static final int sleepTime = 2000;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choosecountry_button);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);//软键盘设置

        Intent i_getvalue = getIntent();
        String action = i_getvalue.getAction();

        if(Intent.ACTION_VIEW.equals(action)){
            Uri uri = i_getvalue.getData();
            if(uri != null){
                String name = uri.getQueryParameter("name");
                String age = uri.getQueryParameter("age");
                Log.e("text",name+"//////"+age);
            }
        }


        //透明状态栏
        initView();
        initEvent();
    }
    @Override
    protected void initView() {
        rootLayout = (RelativeLayout) findViewById(R.id.splash_root);
        choose_country = (RippleBackground) findViewById(R.id.choose_country);
        icon_position = (TextView) findViewById(R.id.icon_position);
        Typeface iconfont = Typeface.createFromAsset(getAssets(), "iconfont.ttf");
        icon_position.setTypeface(iconfont);
    }

    @Override
    protected void initEvent() {
        String language = PreferenceManager.getInstance().getCurrentLanguage();
        if ("".equals(language)||null==language){
            SharedPreferences mainSharedPreferences = getSharedPreferences(Constant.TAG, Activity.MODE_PRIVATE);
            SharedPreferences.Editor editor = mainSharedPreferences.edit();
            editor.clear();
            editor.putString("isLogin", "0");
            editor.commit();
            icon_position.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //增加点击放大效果
                    startActivity(new Intent(ChooseCountryButtonActivity.this, ChooseCountryActivity.class));
                }
            });
        }else {
            AlphaAnimation animation = new AlphaAnimation(0.3f, 1.0f);
            animation.setDuration(1500);
            rootLayout.startAnimation(animation);
        }

        choose_country.startRippleAnimation();

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onStart() {
        super.onStart();


        new Thread(new Runnable() {
            public void run() {
                String language = PreferenceManager.getInstance().getCurrentLanguage();
                if ("".equals(language)||null==language){
                    SharedPreferences mainSharedPreferences = getSharedPreferences(Constant.TAG, Activity.MODE_PRIVATE);
                    SharedPreferences.Editor editor = mainSharedPreferences.edit();
                    editor.clear();
                    editor.putString("isLogin", "0");
                    editor.commit();
                }else if (SjtHelper.getInstance().isLoggedIn()) {
                    // ** 免登陆情况 加载所有本地群和会话
                    //不是必须的，不加sdk也会自动异步去加载(不会重复加载)；
                    //加上的话保证进了主页面会话和群组都已经load完毕
                    long start = System.currentTimeMillis();
                    EMGroupManager.getInstance().loadAllGroups();
                    EMChatManager.getInstance().loadAllConversations();
                    long costTime = System.currentTimeMillis() - start;
                    //等待sleeptime时长
                    if (sleepTime - costTime > 0) {
                        try {
                            Thread.sleep(sleepTime - costTime);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    //进入主页面
                    startActivity(new Intent(ChooseCountryButtonActivity.this, MainActivity.class));
                    finish();
                }else {
                    try {
                        Thread.sleep(sleepTime);
                    } catch (InterruptedException e) {
                    }
                    startActivity(new Intent(ChooseCountryButtonActivity.this, LoginActivity.class));
                    finish();
                }
            }
        }).start();
    }
}
